const axios = require('../../utils/request')

Page({
    data: {
        addressInfo:{},
        loadInfo:{},
        giftDetail:{}
    },
    onLoad(e){
        if(e.giftInfo){
            this.setData({
                loadInfo:JSON.parse(e.giftInfo)
            })
        }
        this.getGiftDetail()
        if(this.data.giftDetail.addres_id==0) this.getDefaultAddress()
    },
    onChangeAddress(){
        wx.chooseAddress({
            success:res=>{
                if(res.errMsg=="chooseAddress:ok"){
                    wx.showLoading({
                        mask:true,
                        title:'设置中...'
                    })
                    delete res.errMsg
                    axios.post('/api/adress/add',res).then(res=>{
                        if(res.code==1){
                            this.setData({
                                addressInfo:res.data
                            })
                            wx.hideLoading()
                            wx.showToast({
                                title:'设置成功'
                            })
                        }
                    })
                }
            }
        })
    },
    onCopyContact(){
        wx.setClipboardData({
            data:this.data.giftDetail.contact.wechat,
            success:()=>{
                wx.showToast({
                    title:"复制成功",
                    icon:'none'
                })
            }
        })
    },
    onCopyCode(){
        wx.setClipboardData({
            data:this.data.giftDetail.code,
            success:()=>{
                wx.showToast({
                    title:"复制成功",
                    icon:'none'
                })
            }
        })
    },
    onSubmit(){
        if(this.data.loadInfo.type==1){
            axios.post("/api/order/add_addres",{
                id:this.data.loadInfo.id
            }).then(res=>{
                if(res.code==1){
                    wx.showToast({
                        title:'提交地址成功！请耐心等待奖品到货哦。',
                        icon:'none'
                    })
                    this.getGiftDetail()

                    const pages = getCurrentPages()
                    let prePage = pages[pages.length-2]
                    prePage.setData({
                        page:1,
                        rewardList:[],
                        finished:false
                    })
                    prePage.getRewardList()
                }else{
                    wx.showToast({
                        title:'提交地址失败，请稍后再试试吧。',
                        icon:'none'
                    })
                }
            })
        }
        
    },
    toBalance(){
        wx.navigateTo({
            url:'/pages/balance/index'
        })
    },
    getDefaultAddress(){
        axios.post('/api/adress/getdefault').then(res=>{
            if(res.code==1){
                this.setData({
                    addressInfo:res.data
                })
            }else{

            }
        })
    },
    getGiftDetail(){
        axios.post('/api/gift/details',{
            id:this.data.loadInfo.id
        }).then(res=>{
            if(res.code==1){
                this.setData({
                    giftDetail:res.data,
                })
                if(res.data.addres_id!=0){
                    this.setData({
                        addressInfo:res.data.addres
                    })
                }
            }
        })
    }
    
})
